package com.lkbaby.builder;

import java.util.Map;
import java.util.StringJoiner;

public class URLBuilder implements IURLBuilder {

	private String domain = "";
	private String scheme = "";
	private String path = "";
	private Map<String, String> query = null;

	@Override
	public URLBuilder builder() {
		return new URLBuilder();
	}

	@Override
	public URLBuilder setDomain(String s) {
		this.domain = s;
		return this;
	}

	@Override
	public URLBuilder setScheme(boolean isSSl) {
		this.scheme = isSSl ? SSL_SCHEME : NO_SSL_SCHEME;
		return this;
	}

	@Override
	public URLBuilder setPath(String path) {
		this.path = path;
		return this;
	}

	@Override
	public URLBuilder setQuery(Map<String, String> query) {
		this.query = query;
		return this;
	}

	@Override
	public String build() {
		final StringJoiner stringJoiner = new StringJoiner("&");
		query.forEach((key, value) -> stringJoiner.add(key + "=" + value));
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append(scheme).append(domain).append(path).append("?").append(stringJoiner.toString());
		return stringBuilder.toString();
	}
}
